﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

using System.Data; 
using System.Collections.Generic; 
using System.Data; 
using System.Data.SqlClient; 
using System.Text;




namespace NNLInterface
{
    public partial class frmImportCapability : NNLInterface.frmBaseBasic
    {

        #region 初始化
        private DataTable dtCapabilityBas;  //保存工时数据
        private frmCapabilityBas frmCapa;


        public frmImportCapability(frmCapabilityBas frmMain)
        {

            InitializeComponent();
            this.SetToolVisible(common.ToolVisible.ImportData);
            this.CmdSearch.Caption = "读取";
            this.ListGrid.MenuManager = this.barmanager;
            this.logButtonVisibility = false;
            this.barInformation.Visible = false;
            this.frmCapa = frmMain;
        }

        private void frmMothInvImport_Load(object sender, EventArgs e)
        {


        }
         #endregion


        private void frmMothInvImport_ToolButtonClick(object sender, string e)
        {
            try
            {
                switch (e)
                {
                    case "search":
                        if (this.btnExcel.Text == "")
                        {
                            DevExpress.XtraEditors.XtraMessageBox.Show("请选择一个正确的CSV文件!", common.sysParameter.MessageInf, MessageBoxButtons.OK, MessageBoxIcon.Information);
                            this.btnExcel.Focus();
                            return;
                        }

                        Cursor currentCursor = Cursor.Current;
                        Cursor.Current = Cursors.WaitCursor;

                        //读取Excel文件
                        if (!ReadCSVFile(btnExcel.Text)) return;

                        DevExpress.XtraEditors.XtraMessageBox.Show("读取CSV文件成功!\r\n" + "共读取" + ListView.RowCount.ToString() + "笔有效数据", common.sysParameter.MessageInf, MessageBoxButtons.OK, MessageBoxIcon.Information);
                        Cursor.Current = currentCursor;
                        break;
                    case "import":
                        if (!CheckData()) return;
                        if (DevExpress.XtraEditors.XtraMessageBox.Show("确定要导入工时数据吗(Y/N)?", common.sysParameter.MessageInf, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) return;
                        if (!subImportData()) return;
                        DevExpress.XtraEditors.XtraMessageBox.Show("所选择工时数据已成功导入", common.sysParameter.MessageInf, MessageBoxButtons.OK, MessageBoxIcon.Information);
                        this.Close();
                        break;

                    case "print":
                        common.PrintView(this.ListGrid, this.Text);
                        break;
                    case "exit":
                        common.SaveLayout(this.ListView);
                        common.SaveToolBarLayout(this.Name, this.barmanager);
                        this.Dispose();
                        break;
                    case "excel":
                        common.ExportExcel(this.ListView);
                        break;

                    default:
                        {
                            break;
                        }
                }


            }
            catch (Exception ex)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show(ex.Message, common.sysParameter.MessageInf, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

        }

        /// <summary>
        /// 读取CSV文件
        /// </summary>
        /// <param name="fileName">待读取的CSV文件</param>
        /// <param name="sampleFileName">ini文件的样式文件名</param>
        /// <returns></returns>
        private bool ReadCSVFile(string fileName)
        {
            try
            {
                //读取Excel文件
                string strPath = System.IO.Path.GetDirectoryName(fileName);
                string OrignFile = common.GetSolutionPath(Application.StartupPath) + @"Schema.ini";
                strPath = strPath.Substring(strPath.Length - 1) == "\\" ? strPath : strPath + "\\";
                File.Copy(OrignFile, strPath + "Schema.ini", true);
                txtFileWrite.WritefileName(strPath + "Schema.ini", "CapabilityBas.csv",
                                           System.IO.Path.GetFileName(fileName));

                dtCapabilityBas = ExcelImport.ExcelToDS(fileName);
                if (dtCapabilityBas == null) return false;
                File.Delete(strPath + "Schema.ini");

                common.SubShowList(dtCapabilityBas, this.ListGrid, this.ListView, false);
                common.GetResourceString(this.Name,this.ListView);
                common.subIniGrid(this.ListView, common.enumGridStyle.ViewStyle);
                this.ListView.BestFitColumns();
                return true;

            }
            catch (Exception ex)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show(ex.Message, common.sysParameter.MessageInf, MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            finally
            {

            }

        }

        private bool CheckData()
        {
            if (!common.CheckExistsData(this.ListView)) return false;
            return true;
        }

        /// <summary>
        /// 导入CSV文件至APS中
        /// </summary>
        private bool subImportData()
        {
            try
            {
                //删除旧的临时表数据
                string strSQL = "TRUNCATE TABLE CapabilityBasTmp";
                if (!accessdb.RsChange(strSQL)) return false;

                //写入新的数据
                System.Data.SqlClient.SqlBulkCopy sqlbulkcopy = new System.Data.SqlClient.SqlBulkCopy(common.strConnect, SqlBulkCopyOptions.UseInternalTransaction);
                sqlbulkcopy.DestinationTableName = "CapabilityBasTmp";//数据库中的表名 

                sqlbulkcopy.ColumnMappings.Add(dtCapabilityBas.Columns[0].ColumnName, "ItemCode");
                sqlbulkcopy.ColumnMappings.Add(dtCapabilityBas.Columns[1].ColumnName, "CustItemCode");
                sqlbulkcopy.ColumnMappings.Add(dtCapabilityBas.Columns[2].ColumnName, "CustNo");
                sqlbulkcopy.ColumnMappings.Add(dtCapabilityBas.Columns[3].ColumnName, "DeptNo");
                sqlbulkcopy.ColumnMappings.Add(dtCapabilityBas.Columns[4].ColumnName, "Z1Capability");
                sqlbulkcopy.ColumnMappings.Add(dtCapabilityBas.Columns[5].ColumnName, "Z2Capability");
                sqlbulkcopy.ColumnMappings.Add(dtCapabilityBas.Columns[6].ColumnName, "Z3Capability");
                sqlbulkcopy.ColumnMappings.Add(dtCapabilityBas.Columns[7].ColumnName, "Z5Capability");
                sqlbulkcopy.ColumnMappings.Add(dtCapabilityBas.Columns[8].ColumnName, "Z6Capability");
                sqlbulkcopy.ColumnMappings.Add(dtCapabilityBas.Columns[9].ColumnName, "Z7Capability");
                sqlbulkcopy.WriteToServer(dtCapabilityBas);

                //更新数据，先删除以父品目为准的BOM数据
                if (!accessdb.RsChange("exec  pro_UpdateCapability")) return false;

                return true;
            }
            catch (Exception ex)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show(ex.Message, common.sysParameter.MessageInf, MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }

        }


        private void btnExcel_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            this.btnExcel.Text = common.ShowOpenFileDialog("打开Excel文档", "Microsoft CSV(*.csv)|*.csv");

        }
    }
}
